﻿using System;

namespace Rsdn.SmartApp.CommandLine
{
    /// <summary>
    /// Strategy for registering command line rules provider.
    /// </summary>
    public class RegCmdLineRulesProvidersStrategy
        : RegElementsStrategy<CommandLineRulesProviderInfo, CommandLineRulesProviderAttribute>
    {
        /// <summary>
        /// Инициализирует экземпляр.
        /// </summary>
        public RegCmdLineRulesProvidersStrategy(IServicePublisher publisher)
            : base(publisher)
        {
        }

        /// <summary>
        /// Creates the element.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="attr">The attr.</param>
        /// <returns></returns>
        /// <exception cref="ExtensibilityException"><c>ExtensibilityException</c>.</exception>
        public override CommandLineRulesProviderInfo CreateElement(
            IExtensionAttachmentContext<Type, Attribute> context,
            CommandLineRulesProviderAttribute attr)
        {
            var contract = typeof(ICommandLineRulesProvider);
            if (contract.IsAssignableFrom(context.ExtensionType))
            {
                throw new ExtensibilityException(
                    "Type '{0}' must implement interface '{1}".FormatStr(context.ExtensionType, contract));
            }

            return new CommandLineRulesProviderInfo(context.ExtensionType);
        }
    }
}